﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnBLL;
using System.Web.Security;
using DoAn.Models;

namespace DoAn.Controllers
{ 
    public class TaiKhoanController : Controller
    {
        private BLUSIEntities db = new BLUSIEntities();

        //
        // GET: /TaiKhoan/

        public ViewResult Index()
        {
            return View(db.taikhoan.ToList());
        }

        //
        // GET: /TaiKhoan/Details/5

        public ViewResult Details(string id)
        {
            taikhoan taikhoan = db.taikhoan.Single(t => t.USERNAME == id);
            return View(taikhoan);
        }

        public ActionResult DangNhap()
        {
            return View();
        }

        [HttpPost]
        public ActionResult DangNhap(TTDangNhap model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.Username, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.Username,model.remember);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Username hoặc Password sai.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /TaiKhoan/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /TaiKhoan/Create

        [HttpPost]
        public ActionResult Create(taikhoan taikhoan)
        {
            if (ModelState.IsValid)
            {
                db.taikhoan.AddObject(taikhoan);
                db.SaveChanges();

                MembershipCreateStatus createStatus;
                Membership.CreateUser(taikhoan.USERNAME,taikhoan.PASS,null, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(taikhoan.USERNAME, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }

                return RedirectToAction("../TTNgDung/Edit");  
            }

            return View(taikhoan);
        }
        
        //
        // GET: /TaiKhoan/Edit/5
 
        public ActionResult Edit(string id)
        {
            taikhoan taikhoan = db.taikhoan.Single(t => t.USERNAME == id);
            return View(taikhoan);
        }

        //
        // POST: /TaiKhoan/Edit/5

        [HttpPost]
        public ActionResult Edit(taikhoan taikhoan)
        {
            if (ModelState.IsValid)
            {
                db.taikhoan.Attach(taikhoan);
                db.ObjectStateManager.ChangeObjectState(taikhoan, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(taikhoan);
        }

        //
        // GET: /TaiKhoan/Delete/5
 
        public ActionResult Delete(string id)
        {
            taikhoan taikhoan = db.taikhoan.Single(t => t.USERNAME == id);
            return View(taikhoan);
        }

        //
        // POST: /TaiKhoan/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            taikhoan taikhoan = db.taikhoan.Single(t => t.USERNAME == id);
            db.taikhoan.DeleteObject(taikhoan);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #region Status Codes
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion
    }
}